﻿using System;
using System.Xml.Linq;

namespace CoreEveMarkets
{
	public static class ProfilProvider
	{
		private static ProfilDll _profil;

		/// <summary>
		/// Création du profil
		/// </summary>
		/// <param name="industry"></param>
		/// <param name="prodEff"></param>
		/// <param name="nom"></param>
		/// <param name="staAchat"></param>
		/// <param name="staVente"></param>
		public static void CreateProfil(int industry, int prodEff, string nom, string staVente, string staAchat)
		{
			_profil = new ProfilDll();
			_profil.NomProfil = nom;
			_profil.SkillIndustry = industry;
			_profil.SkillProdEfficiency = prodEff;
			_profil.StationAchat = staAchat;
			_profil.StationVente = staVente;

			// TODO : A mettre en place plus tard.
			const int implant = 1;
			const int prodSlot = 1;
			_profil.ProductionTimeModifier = (1 - (0.04*industry))*implant*prodSlot;
		}

		/// <summary>
		/// Charge un profil (format XML)
		/// </summary>
		/// <param name="profilXml"></param>
		public static void ChargerProfil(XElement profilXml)
		{
			string nom = (string)profilXml.Element(XName.Get("NOM"));
			int industry = Convert.ToInt32(profilXml.Element(XName.Get("INDUSTRY")).Value);
			int prod = Convert.ToInt32(profilXml.Element(XName.Get("PRODEFF")).Value);
			string vente = (string) profilXml.Element(XName.Get("STAVENTE"));
			string achat = (string) profilXml.Element(XName.Get("STAACHAT"));

			CreateProfil(industry, prod, nom,vente,achat);
		}

		/// <summary>
		/// Charge un profil (Base de donnée)
		/// </summary>
		/// <param name="nomProf"></param>
		/// <param name="skillIndustrie"></param>
		/// <param name="skillProduction"></param>
		/// <param name="stationAchat"></param>
		/// <param name="stationVente"></param>
		public static void ChargerProfil(string nomProf, int skillIndustrie, int skillProduction, string stationAchat, string stationVente)
		{
			CreateProfil(skillIndustrie, skillProduction, nomProf, stationVente, stationAchat);
		}


		/// <summary>
		/// Méthode qui permet de sauvegarder le profil au format XML.
		/// </summary>
		public static void SauvegarderProfil()
		{
			XDocument profilJoueur = new XDocument(
				new XElement("PROFIL",
							 new XElement("NOM", _profil.NomProfil),
							 new XElement("INDUSTRY", _profil.SkillIndustry),
							 new XElement("PRODEFF", _profil.SkillProdEfficiency),
							 new XElement("STAVENTE", _profil.StationVente),
							 new XElement("STAACHAT", _profil.StationAchat)));

			profilJoueur.Save(_profil.NomProfil + ".xml");
		}

		/// <summary>
		/// Retourne le profil en cours, sinon un profil généré
		/// </summary>
		/// <returns></returns>
		public static ProfilDll GetProfil()
		{
			if (_profil != null) 
				return _profil;

			CreateProfil(0, 0, "PasProfil", "","");
			return _profil;
		}

	}
}
